package cn.getech.data.development.controller;

import cn.getech.data.development.entity.Alarm;
import cn.getech.data.development.model.dto.AlarmNotifyDto;
import cn.getech.data.development.model.dto.AlarmSaveDto;
import cn.getech.data.development.model.dto.BdpAlarmLogSearchDto;
import cn.getech.data.development.model.vo.AlarmSearchVO;
import cn.getech.data.development.model.vo.AlarmVO;
import cn.getech.data.development.service.AlarmObjectService;
import cn.getech.data.development.service.AlarmReceivedService;
import cn.getech.data.development.service.AlarmService;
import cn.getech.data.development.service.IBdpAlarmLogService;
import cn.getech.data.intelligence.common.utils.Constant;
import cn.getech.data.intelligence.common.utils.R;
import cn.getech.data.intelligence.common.validator.ValidatorUtils;
import cn.getech.system.center.annotation.SysLog;
import cn.getech.system.center.constant.OperationModulesEnum;
import cn.getech.system.center.constant.OperationTypeEnum;
import cn.getech.system.center.constant.module.ModuleEnum;
import cn.getech.system.center.entity.SysUserEntity;
import cn.getech.system.center.utils.ShiroUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import feign.Param;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.util.Collection;
import java.util.List;

/**
 * 告警管理
 */
@Slf4j
@Api(tags = "告警管理")
@RestController
@RequestMapping("/bdp/alarm")
public class AlarmController {

    @Autowired
    private AlarmService alarmService;
    @Autowired
    private AlarmReceivedService alarmReceivedService;
    @Autowired
    private AlarmObjectService alarmObjectService;

    @Autowired
    private IBdpAlarmLogService bdpAlarmLogService;


    @ApiOperation("获取离线告警分页列表")
    @ApiResponses({@ApiResponse(code = 200, message = "Success", response = AlarmVO.class)})
    @GetMapping("/page")
    //@SysLog(value="获取告警分页列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:alarm:page"})
    public R page(AlarmSearchVO vo) {
        log.info("/page参数接收:[{}]", JSON.toJSONString(vo));
        return R.okWithPage(alarmService.alarmPage(vo));
    }


    /**
     *   报警信息列表
     */
    @ApiOperation("离线报警信息列表")
    @PostMapping("/log/list")
    //@SysLog(value="报警信息列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R list(@RequestBody BdpAlarmLogSearchDto searchDto) {
        ValidatorUtils.validateEntity(searchDto);
        Page page = bdpAlarmLogService.listByPage(searchDto);
        return R.okWithData(page);
    }

    /**
     *   接收人列表
     */
    @ApiOperation("离线接收人列表")
    @GetMapping("/receiver/list")
    //@SysLog(value="接收人列表",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R receiverList() {
        Collection<SysUserEntity> list = bdpAlarmLogService.getReceiverList();
        return R.okWithData(list);
    }


    @ApiOperation("获取告离线警触发方式列表")
    @PostMapping("/startModes")
    //@SysLog(value="获取告警触发方式列表",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:alarm:page"})
    public R startList() {
        JSONArray array = new JSONArray();
        JSONObject object;
        Constant.AlarmStartModeEnum[] startModeEnums = Constant.AlarmStartModeEnum.values();
        for (Constant.AlarmStartModeEnum startModeEnum : startModeEnums) {
            object = new JSONObject();
            object.put("code", startModeEnum.getCode());
            object.put("name", startModeEnum.getName());
            array.add(object);
        }
        return R.okWithData(array);
    }

    @ApiOperation("获取离线告警方式")
    @PostMapping("/alarmModes")
    //@SysLog(value="获取告警方式",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:alarm:page"})
    public R alarmModes() {
        JSONArray array = new JSONArray();
        JSONObject object;
        Constant.AlarmModeEnum[] alarmModeEnums = Constant.AlarmModeEnum.values();
        for (Constant.AlarmModeEnum item : alarmModeEnums) {
            object = new JSONObject();
            object.put("code", item.getCode());
            object.put("name", item.getName());
            array.add(object);
        }
        return R.okWithData(array);
    }

    @ApiOperation("根据id获取离线详情")
    @GetMapping("/info/{id}")
    @SysLog(value="获取离线告警信息详情",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:alarm:page"})
    public R getInfoById(@PathVariable("id") Integer id) {
        return R.okWithData(alarmService.getInfoById(id));
    }

    @ApiOperation("保存离线告警信息")
    @PostMapping("/save")
    @SysLog(value="保存离线告警信息",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:alarm:save"})
    public R saveAlarm(@RequestBody AlarmSaveDto alarmSaveDto) {
        alarmService.saveOrUpdate(alarmSaveDto);
        return R.ok();
    }

    @ApiOperation("根据告警对象id获取告警通知接收人")
    @PostMapping("/getRecevicesByObjectId")
    //@SysLog(value="根据告警对象id获取告警通知接收人",type= OperationTypeEnum.SELECTCODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R saveAlarm(@RequestBody List<Integer> ids) {
        List<AlarmNotifyDto> alarmNotifyUsers = alarmService.getAlarmNotifyUsers(ids);
        return R.okWithData(alarmNotifyUsers);
    }

    @ApiOperation("启用/禁用离线告警信息")
    @GetMapping("/startUsing/{id}/{state}")
    @SysLog(value="启用/禁用离线告警信息",type= OperationTypeEnum.OPENCLOSECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R delete(@PathVariable("id") Integer id, @PathVariable("state") Integer state) {
        if (state == Constant.StateEnum.DISABLE.getValue() || state == Constant.StateEnum.ENABLE.getValue()) {
            Alarm alarm = new Alarm();
            alarm.setId(id);
            alarm.setState(state);
            alarmService.updateById(alarm);
        } else {
            R.error("请输入正确状态");
        }
        return R.ok();
    }

    @ApiOperation("删除离线告警信息")
    @GetMapping("/delete/{id}")
    @SysLog(value="删除离线告警信息",type= OperationTypeEnum.DELETECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    //todo 0925
    //@RequiresPermissions({"bdp:alarm:delete"})
    public R delete(@PathVariable Integer id) {
        if (alarmService.removeById(id)) {
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("alarm_id", id);
            // 删除接收人
            alarmReceivedService.remove(wrapper);
            // 删除告警对象
            alarmObjectService.remove(wrapper);
        }
        return R.ok();
    }



    //获取全部接收人列表
    @ApiOperation("获取接收人列表")
    @GetMapping("/GetReceiver/list")
    //@SysLog(value="接收人列表",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R GetReceiverList() {
        List<AlarmNotifyDto> list = alarmService.getRecriverList();
        JSONArray array = new JSONArray();
        JSONObject object;
        for(AlarmNotifyDto alarmNotifyDto:list ){
            object = new JSONObject();
            object.put("userId", alarmNotifyDto.getUserId());
            object.put("userName", alarmNotifyDto.getUserName());
            array.add(object);
        }
        return R.okWithData(array);
    }

//    //获取全部创建人列表
//    @ApiOperation("根据接收人获取告警列表")
//    @PostMapping("getAlarmInfoByRecriverId")
//    //@SysLog(value="接收人列表",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
//    public R getAlarmInfoByRecriverId(AlarmSearchVO vo) {
//        log.info("/getAlarmInfoByRecriverId参数接收:[{}]", JSON.toJSONString(vo));
//        vo.setUserId(ShiroUtils.getUserId());
//        return R.okWithPage(alarmService.alarmPage(vo));
//    }


    //获取全部创建人列表
    @ApiOperation("获取创建人列表")
    @GetMapping("/getCreater/list")
    //@SysLog(value="接收人列表",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
    public R getCreaterList() {
        List<AlarmNotifyDto> list = alarmService.getCreaterList();
        JSONArray array = new JSONArray();
        JSONObject object;
        for(AlarmNotifyDto alarmNotifyDto:list ){
            object = new JSONObject();
            object.put("userId", alarmNotifyDto.getUserId());
            object.put("userName", alarmNotifyDto.getUserName());
            array.add(object);
        }
        return R.okWithData(array);
    }


//    //获取全部创建人列表
//    @ApiOperation("根据创建人获取告警列表")
//    @PostMapping("getAlarmInfoByCreaterId")
//    //@SysLog(value="接收人列表",type= OperationTypeEnum.UPDATECODE, modulesType = OperationModulesEnum.SYSMODECODE,platformModuleType = ModuleEnum.OPERATION_CENTER_MODULE_CODE)
//    public R getAlarmInfoByCreaterId(AlarmSearchVO vo) {
//        log.info("/getAlarmInfoByCreaterId参数接收:[{}]", JSON.toJSONString(vo));
//        vo.setUserId(ShiroUtils.getUserId());
//        return R.okWithPage(alarmService.alarmPage(vo));
//    }

}
